﻿using KnifeZ.PersonalNotePad.ViewModels;
using KnifeZ.XamarinDomain;
using KnifeZ.XamarinDomain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace KnifeZ.PersonalNotePad.Views.Habitica
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class HomePage : ContentPage
    {
        TodoItemViewModel viewModel;
        public HomePage()
        {
            InitializeComponent();
            BindingContext = viewModel = new TodoItemViewModel();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            if (viewModel.Items.Count == 0)
                viewModel.IsBusy = true;
        }

        private async void Remove_Clicked(object sender, EventArgs e)
        {
            bool answer = await DisplayAlert("确定要删除吗？", "此操作不可恢复", "确定", "取消");
            //DisplayPromptAsync("确定要删除吗？", "该操作不可恢复！！！", "确定", "取消");
            if (answer)
            {
                var layout = (BindableObject)sender;
                var item = (TodoItem)layout.BindingContext;
                await viewModel.DB.DeleteAsync<TodoItem>(item.ID);
            }
        }


        async void AddTask_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new NavigationPage(new NewItemPage()));
        }

    }
}